<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\LihuaUserbonus;
use app\common\model\User;
use app\common\model\LihuaBonus;
use app\common\model\goods\LihuaGoods;

/**
 * 我的红包
 */
class Userbonus extends Api
{
    protected $noNeedLogin = ['sendbonus','sendbonus_multiple'];
    protected $noNeedRight = ['*'];

    /**
     * 红包列表
     *
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="city_id", type="int", required=true, description="城市ID")
     * @ApiParams   (name="type", type="int", required=true, description="1有效，2失效，默认为1")
     * @ApiParams   (name="page", type="int", required=true, description="当前页，默认第1页")
     * @ApiParams   (name="page_size", type="int", required=false, description="每页条数，默认20条")
     * @ApiReturn   ({"code":1,"msg":"OK","time":"1569220820","data":{"page":1,"total":13,"page_size":10,"total_page":2,"list":[{"id":16,"name":"5元红包","type":2,"reach":25,"reduce":5,"des":"满25元减5元","start_date":"2019-09-21","end_date":"2019-10-19"},{"id":15,"name":"3元红包","type":3,"reach":0,"reduce":3,"des":"无门槛限制","start_date":"2019-09-21","end_date":"2019-11-30"},{"id":14,"name":"天降红包","type":3,"reach":0,"reduce":8,"des":"无门槛限制","start_date":"2019-09-21","end_date":"2019-10-30"},{"id":13,"name":"10元红包","type":2,"reach":45,"reduce":10,"des":"满45元减10元","start_date":"2019-09-21","end_date":"2019-11-30"},{"id":12,"name":"开心转盘红包","type":3,"reach":0,"reduce":5,"des":"无门槛限制","start_date":"2019-09-21","end_date":"2019-10-28"},{"id":11,"name":"10元通用红包","type":2,"reach":30,"reduce":10,"des":"满30元减10元","start_date":"2019-09-21","end_date":"2019-11-22"},{"id":10,"name":"天降红包","type":2,"reach":40,"reduce":7,"des":"满40元减7元","start_date":"2019-09-21","end_date":"2019-11-01"},{"id":8,"name":"5元红包","type":3,"reach":0,"reduce":5,"des":"无门槛限制","start_date":"2019-09-21","end_date":"2019-11-15"},{"id":6,"name":"8元红包","type":2,"reach":35,"reduce":8,"des":"满35元减8元","start_date":"2019-09-21","end_date":"2019-11-30"},{"id":5,"name":"5元通用红包","type":2,"reach":25,"reduce":5,"des":"满25元减5元","start_date":"2019-09-21","end_date":"2019-11-18"}]}})
     */
    public function index()
    {
        $city_id   = (int)$this->request->post('city_id');
        if (!$city_id) $this->error('参数错误');
        $type      = (int)$this->request->post('type', 1);
        $page      = max(1, (int)$this->request->post('page'));
        $page_size = (int)$this->request->post('page_size') ? : 20;
        $enough    = (int)$this->request->post('enough', 1); //是否使用满减红包
        $data      = [
            'page'       => $page,
            'total'      => 0,
            'page_size'  => $page_size,
            'total_page' => 0,
            'list'       => []
        ];

        LihuaUserbonus::updateExpireBonusStatus($this->auth->id);//更新失效红包状态

        $where['user_id'] = $this->auth->id;
        $where['city_id'] = ['in', [0, $city_id]];
		
        if ($type == 1) {
            $where['status'] = 0;
        } else {
            $where['status'] = ['>', 0];
        }
        if($enough==0){
            $where['reach'] = ['=', 0];
        }
        $total = LihuaUserbonus::where($where)->count();
        $s = LihuaUserbonus::getLastSql();
        if ($total) {
            $data['total']      = $total;
            $data['total_page'] = ceil($total/$page_size);
            $list = LihuaUserbonus::where($where)
                ->field('id,name,type,reach,reduce,des,start_date,end_date,goods')
                ->order('id', 'desc')->page($page, $page_size)->select();
            foreach ($list as $k => $v) {
                $list[$k]['reach']  = round($v['reach'], 2);
                $list[$k]['reduce'] = round($v['reduce'], 2);
                if (!$list[$k]['des']) {
                    if ($list[$k]['reach'] == 0) {
                        $list[$k]['des'] = '无门槛限制';
                    } else {
                        $list[$k]['des'] = '满' . $list[$k]['reach'] . '元减' . $list[$k]['reduce'] . '元';
                    }
                }
                //如果是指定商品红包，需要查询出该商品详情
                $goodsarr = LihuaGoods::where(['id'=> ['in', $v['goods']]])->select();
                $list[$k]['goodsarr'] = $goodsarr;
            }
            $data['list'] = $list;
        }
		$thisday = date("Y/m/d");
		if( $city_id==3 && ($thisday == "2020/01/24" || $thisday == "2020/01/25" || $thisday == "2020/01/26" || $thisday == "2020/01/27" || $thisday == "2020/01/28" || $thisday == "2020/01/29" || $thisday == "2020/01/30")){
			$data['list'] = [];
			$data['total'] = 0;
			$data['total_page'] = 0;
		}
        $this->success('OK', $data);
    }

    public function sendbonus_multiple()
    {
        $mobile  = $this->request->request('mobile', 0); //用户手机号
        $city_id = $this->request->request('city_id', ""); //城市信息
        $bonus_id = $this->request->request('bonus_id', ""); //红包ID
        $num = $this->request->request('num', 1); //红包ID
        $start_date = $this->request->request('start_time', date('Y-m-d')); //开始时间
        $end_date = $this->request->request('end_time', date("Y-m-d", strtotime("+10 day"))); //开始时间
        $bonus_id_arr = explode(",",$bonus_id);
        $mobile_arr = explode(",",$mobile);
        foreach ($mobile_arr as $k => $v) {
            $exists = User::where('mobile', $v)->find();
            if(!!$exists) {
                foreach($bonus_id_arr as $k2 => $v2){
                    $bonus = LihuaBonus::where('id', $v2)->find();
                    if(!!$bonus){
                        for ($x=0; $x<$num; $x++) {
                            $create_data = [
                                'city_id'    => $city_id,
                                'user_id'    => $exists['id'],
                                'bonus_id'   => $bonus['id'],
                                'name'       => $bonus['name'],
                                'type'       => $bonus['type'],
                                'reach'      => $bonus['reach'],
                                'reduce'     => $bonus['reduce'],
                                'des'        => $bonus['des'],
                                'start_date' => $start_date,
                                'end_date'   => $end_date,
                            ];
                            $ret = LihuaUserbonus::create($create_data);
                        } 
                        
                    }
                }
            }
        }
        $this->success('OK', $mobile_arr);
    }

    public function sendbonus()
    {
        $mobile  = $this->request->request('mobile', 0); //用户手机号
        $city_id = $this->request->request('city_id', ""); //城市信息
        $bonus_id = $this->request->request('bonus_id', ""); //红包ID
        $start_date = $this->request->request('start_time', date('Y-m-d')); //开始时间
        $end_date = $this->request->request('end_time', date("Y-m-d", strtotime("+10 day"))); //开始时间

        $exists = User::where('mobile', $mobile)->find();
        if(!$exists) $this->error('此用户还未注册');

        $bonus = LihuaBonus::where('id', $bonus_id)->find();
        $s = LihuaBonus::getLastSql();
        if(!$bonus) $this->error('此红包不存在');
        $ret = LihuaUserbonus::create([
            'city_id'    => $city_id,
            'user_id'    => $exists['id'],
            'bonus_id'   => $bonus['id'],
            'name'       => $bonus['name'],
            'type'       => $bonus['type'],
            'reach'      => $bonus['reach'],
            'reduce'     => $bonus['reduce'],
            'des'        => $bonus['des'],
            'start_date' => $start_date,
            'end_date'   => $end_date,
        ]);
        if($ret) $this->success('OK', $ret);
    }

}
